<?php

namespace app\wechat\controller;

use app\admin\model\Authorizer;
use app\admin\model\CustomerMessage;
use think\cache\driver\Redis;

// 公众号用户openid相关
class Openid
{
    public $redis;
    public $handler;

    public function __construct($persistent = false, $select = 0)
    {
        $this->redis = new Redis($options = ['persistent' => $persistent, 'select' => $select]);
        $this->handler = $this->redis->handler(); // 获取redis句柄对象, 用于执行其他高级方法
    }

    // 存入全部openid
    public function set_all_openid($appid, $next_openid = null)
    {
        $platform = new Platform();
        $app = $platform->getGh($appid);
        $res = $app->user->list($next_openid);

        // 判断返回结果,并分析,若未结束,继续获取openid
        try {
            $this->handler->sAddArray($appid . '_test_openid', $res['data']['openid']);
            if ($res['count'] != 10000) {
                return 'ok';
            } else {
                $this->set_all_openid($appid, $next_openid = $res['next_openid']);
            }
        } catch (\Exception $e) {
            return json($e->getMessage());
        }
    }

    // 存openid
    public function add_openid($appid, $openid)
    {
        $this->handler->sAdd($appid . '_openid', $openid);
    }

    // 获取当前openid数量
    public function get_openid_num($appid)
    {
        return $this->handler->sCard($appid . '_openid');
    }

    // 获取发送成功数量
    public function get_suc_num($appid)
    {
        return $this->handler->sCard($appid . '_suc');
    }

    // 获取当前消息下绑定公众号所有的openid数量
    public function get_all_openid_num($msgid)
    {
        $gh_list = CustomerMessage::get($msgid)->gh_list;
        $auth_list = Authorizer::whereIn('ghid', $gh_list)->select();
        $count = 0;
        foreach ($auth_list as $auth) {
            $num = $this->get_openid_num($auth->appid);
            $count += $num;
        }
        return $count;
    }

    // 获取当前消息下绑定公众号所有的发送成功的数量
    public function get_all_suc_num($msgid)
    {
        $gh_list = CustomerMessage::get($msgid)->gh_list;
        $auth_list = Authorizer::whereIn('ghid', $gh_list)->select();
        $count = 0;
        foreach ($auth_list as $auth) {
            $num = $this->get_suc_num($auth->appid);
            $count += $num;
        }
        return $count;
    }

    // 随机拿一个openid
    public function pop_openid($appid)
    {
        return $this->handler->sPop($appid . '_openid');
    }

    // 将成功的openid导入到openid中
    public function merge_openid($appid)
    {
        $openid = $this->handler->sPop($appid . '_suc');
        while ($openid) {
            $this->add_openid($appid, $openid);
            $openid = $this->handler->sPop($appid . '_suc');
        }
        return 'ok';
    }

    // 存储发送成功的openid
    public function count($appid, $openid)
    {
        $this->handler->sAdd($appid . '_suc', $openid);
    }

    // 存储获取成功的粉丝信息
    public function save_fans_info($appid, $data)
    {
        $this->handler->sAdd($appid . '_fans_info', $data);
    }

    // 获取俩个粉丝信息的交集
    public function get_fans_intersection($appid1,$appid2)
    {
        return $this->handler->sInter($appid1 . '_fans_info',$appid2 . '_fans_info');
    }

    // 获取成功粉丝信息的数量
    public function get_fans_info_count($appid)
    {
        return $this->handler->sCard($appid . '_fans_info');
    }

    // 删除某个公众号粉丝数据
    public function del_fans_info($appid)
    {
        return $this->handler->del($appid . '_fans_info');
    }

    // 清空set
    public function del_set($key)
    {
        $this->handler->del($key);
    }

    // 处理公众号已关注粉丝数量
    public function fans_num($appid, $type = 'get', $data = null)
    {
        switch ($type) {
            case 'set': return $this->redis->set($appid . '_fans_num', $data);
            case 'get': return $this->redis->get($appid . '_fans_num');
            case 'rm': return $this->redis->rm($appid);
        }
    }

}
